package com.chengh.nami.common.domain.model;

import com.chengh.nami.common.domain.pojo.SysMenu;
import com.chengh.nami.common.domain.pojo.SysRole;
import com.chengh.nami.common.domain.pojo.SysUser;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * @author chengh
 * SpringSecurity用户的实体
 */
@Getter
@Setter
@NoArgsConstructor
@ToString(callSuper = true)
@Slf4j
public class LoginUserEntity implements Serializable, UserDetails {

    private static final long serialVersionUID = 1L;

    /**
     * 用户ID
     */
    private Long id;

    /**
     * 部门ID
     */
    private Long deptId;

    /**
     * 用户名
     */
    private String username;

    /**
     * 用户密码
     */
    private String password;

    /**
     * token
     */
    private String token;

    /**
     * 登录时间
     */
    private Long loginTime;

    /**
     * 过期时间
     */
    private Long expireTime;

    /**
     * 登录IP地址
     */
    private String ipaddr;

    /**
     * 登录地点
     */
    private String loginLocation;

    /**
     * 浏览器类型
     */
    private String browser;

    /**
     * 操作系统
     */
    private String os;

    /**
     * 用户信息
     */
    private SysUser user;

    /**
     * 用户角色
     */
    private List<SysRole> roles;

    /**
     * 用户菜单,树型结构
     */
    private List<SysMenu> menus;

    /**
     * 用户权限
     */
    private Set<String> permissions;

    /**
     * 账户是否过期
     */
    private boolean isAccountNonExpired = true;
    /**
     * 账户是否被锁定
     */
    private boolean isAccountNonLocked = true;
    /**
     * 证书是否过期
     */
    private boolean isCredentialsNonExpired = true;
    /**
     * 账户是否有效
     */
    private boolean isEnabled = true;

    public LoginUserEntity(Long id, String username, String password, Long deptId, SysUser sysUser, List<SysRole> roles, List<SysMenu> menus, Set<String> permissions) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.deptId = deptId;
        this.user = sysUser;
        this.roles = roles;
        this.menus = menus;
        this.permissions = permissions;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    /**
     * 账号是否过期
     */
    @Override
    public boolean isAccountNonExpired() {
        return this.isAccountNonExpired;
    }

    /**
     * 账号是否被锁定
     */
    @Override
    public boolean isAccountNonLocked() {
        return this.isAccountNonLocked;
    }

    /**
     * 凭证是否过期
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return this.isCredentialsNonExpired;
    }

    /**
     * 是否禁用
     */
    @Override
    public boolean isEnabled() {
        return this.isEnabled;
    }

}
